<?php

namespace App\Admin\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\crm_deal;
use App\crm_order;
use App\crm_users;
use App\crm_customer;
use Encore\Admin\Auth\Database\Permission;
use App\Admin\Extensions\Tools\BumenGender;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\JiesuanGender;

class Crm_hesuanController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content){
            $content->header('核算单统计');
            $content->description('统计');
            $yuangong=crm_users::all();//所有的员工
            $bumen=Permission::all();//所有的部门
            $kehu=crm_customer::all();//客户
            $order=crm_order::all();//订单线路
            $list=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->paginate(1);
            $maoli=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->get();
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $a[$k]=$vo->deal_lirun;
                }
                foreach($maoli as $k=>$vo){
                    $b[$k]=$vo->deal_sshoukuan;
                }
                foreach($maoli as $k=>$vo){
                    $c[$k]=$vo->deal_sdakuan;
                }
                $ygzongji=array_sum($a);
                $yshoukuan=array_sum($b);
                $dakuan=array_sum($c);
            }else{
                $ygzongji=0;
                $yshoukuan=0;
                $dakuan=0;
            }
            $res['list']=$list;
            $res['customer']=$kehu;
            $res['order']=$order;
            $res['ygzongji']=$ygzongji;
            $res['bumen']=$bumen;
            $res['yuangong']=$yuangong;
            $res['yshoukuan']=$yshoukuan;
            $res['dakuan']=$dakuan;
            $content->body(new JiesuanGender($res));
        });
    }
    // 表单提交后
    public function chengjiao(Request $request)
    {
        return Admin::content(function (Content $content) use ($request) {
            $content->header('核算单统计');
            $content->description('统计');
            $yuangong=crm_users::all();//所有的员工
            $bumen=Permission::all();//所有的部门
            $list=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->paginate(1);
            $maoli=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->get();
            $query = DB::table('crm_deal')->where('deal_jiesuanstatue','=',3);
            if($request['bumen']){
                $aa=DB::table('admin_user_permissions')->where("permission_id",$request['bumen'])->pluck("user_id");
                $list=$query->whereIn('deal_userid',$aa)->paginate(1);
                $maoli=$query->whereIn('deal_userid',$aa)->get();
            }
            if($request['yuangong']){
                $list=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->where('deal_userid',$request['yuangong'])->paginate(1);
                $maoli=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->where('deal_userid',$request['yuangong'])->get();
                $query=DB::table('crm_deal')->where('deal_jiesuanstatue','=',3)->where('deal_userid',$request['yuangong']);
            }
            if($request['order_lvyoudate']){
                if($request['order_lvyoudate']['start']){
                    $list=$query->where('deal_dealdate', '>', substr($request['order_lvyoudate']['start'], 2))->paginate(1);
                    $maoli=$query->where('deal_dealdate', '>',substr($request['order_lvyoudate']['start'], 2))->get();
                    $query=$query->where('deal_dealdate', '>',substr($request['order_lvyoudate']['start'], 2));
                }
                if($request['order_lvyoudate']['end']){
                    $list=$query->where('deal_dealdate', '<', substr($request['order_lvyoudate']['end'], 2))->paginate(1);
                    $maoli=$query->where('deal_dealdate', '<',substr($request['order_lvyoudate']['end'], 2))->get();
                }
            }
            if(!$maoli->isEmpty()){
                foreach($maoli as $k=>$vo){
                    $a[$k]=$vo->deal_lirun;
                }
                foreach($maoli as $k=>$vo){
                    $b[$k]=$vo->deal_sshoukuan;
                }
                foreach($maoli as $k=>$vo){
                    $c[$k]=$vo->deal_sdakuan;
                }
                $ygzongji=array_sum($a);
                $yshoukuan=array_sum($b);
                $dakuan=array_sum($c);
            }else{
                $ygzongji=0;
                $yshoukuan=0;
                $dakuan=0;
            }
            $kehu=crm_customer::all();
            $order=crm_order::all();
            $res['list']=$list;
            $res['customer']=$kehu;
            $res['order']=$order;
            $res['ygzongji']=$ygzongji;
            $res['bumen']=$bumen;
            $res['yuangong']=$yuangong;
            $res['yshoukuan']=$yshoukuan;
            $res['dakuan']=$dakuan;
            $content->body(new JiesuanGender($res));
        });

    }
  

}
